package com.augurit.gzcgwsp.jzfqwczz.common.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.dozer.DozerBeanMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;

import com.augurit.common.util.HqlUtils;
import com.augurit.common.util.PageUtils;
import com.augurit.common.util.lang.StringUtils;
import com.augurit.gzcgwsp.jzfqwczz.common.convert.YwYnJzfqwczzCsConvertor;
import com.augurit.gzcgwsp.jzfqwczz.common.dao.YwYnJzfqwczzCsDao;
import com.augurit.gzcgwsp.jzfqwczz.common.dao.YwYnJzfqwczzCsDao;
import com.augurit.gzcgwsp.jzfqwczz.common.entity.YwYnJzfqwczzCs;
import com.augurit.gzcgwsp.jzfqwczz.common.service.IYwYnJzfqwczzCsService;
import com.augurit.gzcgwsp.jzfqwczz.common.web.form.YwYnJzfqwczzCsForm;

@Service
@Transactional
public class YwYnJzfqwczzCsServiceImpl implements IYwYnJzfqwczzCsService {
	
	@Autowired
	private YwYnJzfqwczzCsDao YwYnJzfqwczzCsDao;
	
	@Transactional(readOnly = true)
	public YwYnJzfqwczzCsForm get(Long id) {
		YwYnJzfqwczzCs entity = YwYnJzfqwczzCsDao.get(id);
		return YwYnJzfqwczzCsConvertor.convertVoToForm(entity);
	}
	
	public void delete(Long... ids) {
		YwYnJzfqwczzCsDao.delete(ids);
	}

	public void save(YwYnJzfqwczzCsForm... forms) {
		if(forms != null)
			for(YwYnJzfqwczzCsForm form : forms)
				this.save(form);
	}
	
	public void update(YwYnJzfqwczzCsForm form) {
	    YwYnJzfqwczzCsForm dbform = null;
	    if (form.getId() != null && form.getId() != 0) dbform = this.get(form.getId());
	    
	    if (dbform == null) {
	        this.save(form);
	    } else {
	        new DozerBeanMapper().map(form, dbform);
            this.save(dbform);
	    }
	}
	
	public void save(YwYnJzfqwczzCsForm form){
		
		if(form != null){
			YwYnJzfqwczzCs entity = null;
			
			if(form != null && form.getId() != null){
				entity = YwYnJzfqwczzCsDao.getYwYnJzfqwczzCs(form.getId());
				if(entity.getId()==null){
					entity = new YwYnJzfqwczzCs();
					entity.setId(form.getId());
				}
			}else{
				entity = new YwYnJzfqwczzCs();
			}
			
			YwYnJzfqwczzCsConvertor.convertFormToVo(form, entity);
			//如果没有地下室，则设置下面三个属性值为空
			if(org.apache.commons.lang.StringUtils.isNotBlank(entity.getSfydxs()) && "2".endsWith(entity.getSfydxs())){
				entity.setDxsmj(null);
				entity.setDxsqk(null);
				entity.setDxssd(null);
			}
			YwYnJzfqwczzCsDao.save(entity);
			
			form.setId(entity.getId());
		}
	}

	@Transactional(readOnly = true)
	public Page<YwYnJzfqwczzCsForm> search(Page<YwYnJzfqwczzCsForm> page, YwYnJzfqwczzCsForm form) {
		Page pg = PageUtils.newPageInstance(page);
		
		StringBuffer hql = new StringBuffer("from YwYnJzfqwczzCs ps where 1=1");
		List values = new ArrayList();
		
		if(form != null){
		    if(StringUtils.isNotBlank(form.getTplcode())){
		    	hql.append(" and ps.templateCode = '" + form.getTplcode() + "'");
		    }
		}
		
		hql.append(HqlUtils.buildOrderBy(page, "ps"));
		
		YwYnJzfqwczzCsDao.findPage(pg, hql.toString(), values);
		
		List<YwYnJzfqwczzCsForm> list = YwYnJzfqwczzCsConvertor.convertVoListToFormList(pg.getResult());
		PageUtils.copy(pg, list, page);
		return page;
	}
	
	@Transactional(readOnly = true)
	public Page<YwYnJzfqwczzCsForm> search(Page<YwYnJzfqwczzCsForm> page, List<PropertyFilter> filters) {
		Page<YwYnJzfqwczzCs> pg = PageUtils.newPageInstance(page);
		
		YwYnJzfqwczzCsDao.findPage(pg, filters);
		
		List<YwYnJzfqwczzCsForm> list = YwYnJzfqwczzCsConvertor.convertVoListToFormList(pg.getResult());
		PageUtils.copy(pg, list, page);
		return page;
	}

	public void updateYwYnJzfqwczzCs(YwYnJzfqwczzCsForm form) {
	    YwYnJzfqwczzCsForm dbform = null;
	    if (form.getId() != null && form.getId() != 0)
	    	dbform = this.get(form.getId());
	    if (dbform != null) {
			YwYnJzfqwczzCs entity = null;
			entity = YwYnJzfqwczzCsDao.getYwYnJzfqwczzCs(dbform.getId());
			YwYnJzfqwczzCsConvertor.convertFormToVo(form, entity);
			if(entity!=null){
				YwYnJzfqwczzCsDao.updateYwYnJzfqwczzCs(entity);
			}
	    }
	}

    public void saveEntity(YwYnJzfqwczzCs entity) {
       this.YwYnJzfqwczzCsDao.save(entity);
        
    }

	/**
	 * 保存现场勘察表返回id
	 */
	public Long saveEnterpriseInfo(YwYnJzfqwczzCsForm form) {
		YwYnJzfqwczzCs entity =new YwYnJzfqwczzCs();
		
		YwYnJzfqwczzCsConvertor.convertFormToVo(form, entity);
		
		return YwYnJzfqwczzCsDao.saveEntity(entity);
	}
}
